package com.atguigu.juc.lock;

import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @since 2021-07-15 0:40
 */
class LTicket{
    private int number = 30;
    private final ReentrantLock lock = new ReentrantLock();

    public void sale(){
            try {
                lock.lock();
                if (number > 0){
                    System.out.println(Thread.currentThread().getName()+": 卖出了"+number--+" 还剩: "+number);
                }
            }finally {
                lock.unlock();
            }
    }
}

public class LSaleTicket {
    public static void main(String[] args) {
        LTicket ticket = new LTicket();
        new Thread(()->{
            for (int i = 0; i < 40; i++) {
                ticket.sale();
            }
        },"AA").start();
        new Thread(()->{
            for (int i = 0; i < 40; i++) {
                ticket.sale();
            }
        },"BB").start();
        new Thread(()->{
            for (int i = 0; i < 40; i++) {
                ticket.sale();
            }
        },"CC").start();
    }
}
